package com.bong.panpan.bongcharttools.custom;

import com.bong.panpan.bongcharttools.ble.BleLink;

/**
 * Created by panpan on 2017/9/27.
 */

public class MyProtocol {

    private OnProtocalDealListener listener;
    private int receivedBytes = 0;

    public MyProtocol() {

    }

    public void reset() {
        receivedBytes = 0;
    }

    private int byte2Int(byte data) {
        if (data < 0)
            return data + 256;
        return data;
    }

    public void receive(byte... data) {
        switch (byte2Int(data[0])) {
            case 0x2F:
                int chartData[] = new int[(data.length - 1) / 2];//16位数据
                int index = 1;
                for (int i = 0; i < chartData.length; i++) {
                    //chartData[i] = (byte2Int(data[index++]) << 24) | (byte2Int(data[index++]) << 16) | (byte2Int(data[index++]) << 8) | (byte2Int(data[index++]) << 0);
                    chartData[i] = (byte2Int(data[index++]) << 8) | (byte2Int(data[index++]) << 0);
                }
                if (data.length > 20)
                    OnProtocalDealOk(0x2F, chartData, null);
                else
                    OnProtocalDealOk(0x2F, chartData, null);
                break;
            default:
                break;
        }
    }

    public void receive2(byte... data) {
        int chartData[] = null;
        if (data.length > 19) {
            receivedBytes+=data.length;
            int index = 0;
            if (data.length == 20 || data.length == 24) {
                chartData = new int[10];//16位数据
                for (int i = 0; i < chartData.length; i++) {
                    chartData[i] = (byte2Int(data[index++]) << 8) | (byte2Int(data[index++]) << 0);
                }
            } else if (data.length >= 29) {
                int chartDataLength = (data.length - 4) / 5 * 2;
                chartData = new int[chartDataLength];//16位数据
                for (int i = 0; i < chartData.length; i++) {
                    if (i % 2 == 0)
                        chartData[i] = (byte2Int(data[index++]) << 16) | (byte2Int(data[index++]) << 8) | (byte2Int(data[index++]) << 0);
                    else
                        chartData[i] = (byte2Int(data[index++]) << 8) | (byte2Int(data[index++]) << 0);
                }
            }
        }
        if (data.length > 20) {
            if (data.length == 24) {
                int valueData[] = new int[data.length - 20];
                for (int i = 0; i < valueData.length; i++) {
                    valueData[i] = byte2Int(data[20 + i]);
                }
                OnProtocalDealOk(0x2F, chartData, valueData);
            } else if (data.length >= 29) {
                int valueData[] = new int[4];
                for (int i = 0; i < valueData.length; i++) {
                    valueData[i] = byte2Int(data[data.length - 4 + i]);
                }
                OnProtocalDealOk(0x2F, chartData, valueData);
            }
        } else if (data.length == 20) {
            OnProtocalDealOk(0x2F, chartData, null);
        } else if (data.length == 4) {
            int index = 0;
            int bytes = (byte2Int(data[index++]) << 0) | (byte2Int(data[index++]) << 8) | (byte2Int(data[index++]) << 16) | (byte2Int(data[index++]) << 24);
            OnReceivedBytes(bytes,receivedBytes);
        }
    }

    public void OnProtocalDealOk(int cmd, int[] data, int[] valueData) {
        if (listener != null)
            listener.OnProtocalDealOk(cmd, data, valueData);
    }

    public void OnReceivedBytes(int bytes,int bytes2) {
        if (listener != null)
            listener.OnProtocalReceivedBytes(bytes,bytes2);
    }

    public void setListener(OnProtocalDealListener listener) {
        this.listener = listener;
    }

    public interface OnProtocalDealListener {
        void OnProtocalDealOk(int cmd, int[] data, int[] valueData);

        void OnProtocalReceivedBytes(int bytes,int bytes2);

        void OnProtocalReceived(byte... data);
    }

    public static boolean sendData(BleLink link, boolean isBegin, UserInfoRecord record) {
        byte data[] = new byte[6];
        int temp;
        data[0] = 0x2F;
        if (isBegin)
            data[1] = 0x01;
        else
            data[1] = 0x00;
        if (record != null) {
            temp = Integer.valueOf(record.editText.get(2));
            data[2] = (byte) temp;
            temp = Integer.valueOf(record.editText.get(3));
            data[3] = (byte) temp;
            temp = Integer.valueOf(record.editText.get(4));
            data[4] = (byte) temp;
            if (record.radioChecks.get(0))
                data[5] = 0;
            else
                data[5] = 1;
        }
        return link.sendData(data);
    }
}
